<?php
/**
 ***********************************************************************************************************************
 * 获取申通E3系统基础快递员信息
 * @author llq  2015/5/20
 ***********************************************************************************************************************
 */
namespace soa\parter\sto;
use soa\client;
use soa\partner\Counterman;

/**
 * Class courier
 * @package soa\parter\sto
 */
class courier extends client
{
    /**
     * 根据手机号码获取快递员信息
     * @param $mobile
     * @return array|bool
     */
    public function getCourierByMobile($mobile)
    {
        if(empty($mobile) || !is_numeric($mobile) || strlen($mobile) != 11) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','mobile' => $mobile)));
//        return $this->get('v1/parter/sto/getCourierByMobile', array('mobile' => $mobile));
    }

    /**
     * 根据工号获取快递员信息
     * @param $emp_no
     * @return array|bool
     */
    public function getCourierByEmpNo($emp_no)
    {
        if(empty($emp_no) || !is_string($emp_no)) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','emp_no' => $emp_no)),'one');
//        return $this->get('v1/parter/sto/getCourierByEmpNo', array('emp_no' => $emp_no));
    }

    /**
     * 根据多个手机号码获取快递员信息
     * @param array $mobiles
     * @return array|bool
     */
    public function getCourierByMobiles(array $mobiles)
    {
        if(empty($mobiles)) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','mobiles' => $mobiles)));
//        return $this->post('v1/parter/sto/getCourierByMobiles', array('mobiles' => implode(',', $mobiles)));
    }

    /**
     * 根据多个工号获取快递员信息
     * @param array $emp_nos
     * @return array|bool
     */
    public function getCourierByEmpNos(array $emp_nos)
    {
        if(empty($emp_nos)) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','emp_nos' => $emp_nos)));
//        return $this->post('v1/parter/sto/getCourierByEmpNos', array('emp_nos' => implode(',', $emp_nos)));
    }

    /**
     * 根据网点编号批量获取快递员
     * @param $branch_id
     * @return array|bool
     */
    public function getCourierByBranchId($branch_id)
    {
        if(empty($branch_id)) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','branch_id' => $branch_id)));
//        return $this->post('v1/parter/sto/getCourierByBranchId', array('branch_id' => $branch_id));
    }

    /**
     * 根据网点名称批量获取快递员
     * @param $branch_name
     * @return array|bool
     */
    public function getCourierByBranchName($branch_name)
    {
        if(empty($branch_name)) {
            return array();
        }
        return $this->buildCourierReturn((new Counterman())->counterman(array('brand'=>'sto','branch_name' => $branch_name)));
//        return $this->post('v1/parter/sto/getCourierByBranchName', array('branch_name' => $branch_name));
    }

    /**
     * @param $data
     * @param string $type
     * @return array|mixed
     */
    public function buildCourierReturn($data, $type = 'all')
    {
        if($data && is_array($data)){
            $return = [];
            foreach($data as $item){
                if(!empty($item['express_company']) && 'sto' != $item['express_company']){
                    continue;
                }
                $return[] = [
                    'emp_no'      => $item['counterman_code'] ?: '',
                    'emp_name'    => $item['counterman_name'] ?: '',
                    'branch_name' => $item['shop_name'] ?: '',
                    'mobile'      => $item['phone'] ?: '',
                    'role'        => $item['role'] ?: '',
                    'branch_id'   => $item['branch_code'] ?: '',
                    'contact_tel' => $item['tel'] ? : '',
                ];
            }
            if('one' == $type){
                return array_shift($return);
            }
            return $return;
        }else{
            return $data;
        }
    }
}